package com.qiu.demo.config;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.qiu.demo.enums.HttpCodeEnum;
import com.qiu.demo.exception.BusinessException;
import com.qiu.demo.util.JsonResult;

import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常信息处理类
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {

    /**
     * 处理业务异常
     *
     * @param e BusinessException
     * @return businessException
     */
    @ExceptionHandler(value = BusinessException.class)
    public JsonResult businessException(BusinessException e) {
        log.error("业务异常 => code: {}, 原因是: {}", e.getCode(), e.getMessage());
        return JsonResult.error().codeAndMessage(e.getCode(), e.getMessage());
    }

    /**
     * 处理未知(600)异常
     *
     * @param e otherException
     * @return globalException
     */
    @ExceptionHandler(value = Exception.class)
    public JsonResult globalException(Exception e) {
        log.error("未知(600)异常 => 原因是: {}", e.getMessage());
        return JsonResult.error().codeAndMessage(HttpCodeEnum.RC600);
    }

}
